package com.ruoyi.module.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.module.domain.MaterialRecord;
import com.ruoyi.module.mapper.MaterialRecordMapper;
import com.ruoyi.module.service.IMaterialRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.mapper.MaterialCourseMapper;
import com.ruoyi.module.domain.MaterialCourse;
import com.ruoyi.module.service.IMaterialCourseService;

/**
 * 课程内容Service业务层处理
 *
 * @author fubojin
 * @date 2025-03-04
 */
@Service
public class MaterialCourseServiceImpl implements IMaterialCourseService {
    @Autowired
    private MaterialCourseMapper materialCourseMapper;

    @Autowired
    private IMaterialRecordService materialRecordService;

    /**
     * 查询课程内容
     *
     * @param id 课程内容主键
     * @return 课程内容
     */
    @Override
    public MaterialCourse selectMaterialCourseById(Long id, Long userId) {

        //添加个人学习记录
        MaterialRecord materialRecord = materialRecordService.selectMaterialRecordByUserId(userId);
        if (materialRecord == null) {
            materialRecord = new MaterialRecord();
            materialRecord.setLearningFrequency(1);
            materialRecord.setUserId(userId);
            materialRecordService.insertMaterialRecord(materialRecord);
        } else {
            materialRecord.setLearningFrequency(materialRecord.getLearningFrequency() + 1);
            materialRecordService.updateMaterialRecord(materialRecord);
        }

        MaterialCourse materialCourse = materialCourseMapper.selectMaterialCourseById(id);
        materialCourse.setHits(materialCourse.getHits() + 1);
        materialCourseMapper.updateMaterialCourse(materialCourse);
        return materialCourse;
    }

    /**
     * 查询课程内容列表
     *
     * @param materialCourse 课程内容
     * @return 课程内容
     */
    @Override
    public List<MaterialCourse> selectMaterialCourseList(MaterialCourse materialCourse) {
        return materialCourseMapper.selectMaterialCourseList(materialCourse);
    }

    /**
     * 新增课程内容
     *
     * @param materialCourse 课程内容
     * @return 结果
     */
    @Override
    public int insertMaterialCourse(MaterialCourse materialCourse) {
        materialCourse.setCreateTime(DateUtils.getNowDate());
        return materialCourseMapper.insertMaterialCourse(materialCourse);
    }

    /**
     * 修改课程内容
     *
     * @param materialCourse 课程内容
     * @return 结果
     */
    @Override
    public int updateMaterialCourse(MaterialCourse materialCourse) {
        materialCourse.setUpdateTime(DateUtils.getNowDate());
        return materialCourseMapper.updateMaterialCourse(materialCourse);
    }

    /**
     * 批量删除课程内容
     *
     * @param ids 需要删除的课程内容主键
     * @return 结果
     */
    @Override
    public int deleteMaterialCourseByIds(Long[] ids) {
        return materialCourseMapper.deleteMaterialCourseByIds(ids);
    }

    /**
     * 删除课程内容信息
     *
     * @param id 课程内容主键
     * @return 结果
     */
    @Override
    public int deleteMaterialCourseById(Long id) {
        return materialCourseMapper.deleteMaterialCourseById(id);
    }
}
